<template>
  <div class="dashboard-editor-container">
    <el-table :data="tableData" style="width: 100%;text-align: center;">
      <el-table-column prop="userName" label="用户名"></el-table-column>
      <el-table-column prop="date" label="上次登录时间">
        <template slot-scope="scope">
          <span>{{scope.row.date | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="loginIp" label="上次登录IP"></el-table-column>
    </el-table>

    <el-dialog
      :title="textMap[dialogStatus]"
      custom-class="el-dialog--center"
      :close-on-click-modal="false"
      :before-close="ai_dialog_close"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
      width="40%"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item class="w347" label="旧密码" placeholder="请输入新密码" prop="oldPassword">
          <el-input type="password" v-model="form.oldPassword"></el-input>
        </el-form-item>

        <el-form-item class="w347" label="新密码" placeholder="请输入新密码" prop="newPassword">
          <el-input type="password" v-model="form.newPassword"></el-input>
        </el-form-item>

        <el-form-item class="w347" label="确认新密码" placeholder="请输入新密码" prop="confirmNewPassword">
          <el-input type="password" v-model="form.confirmNewPassword"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button @click="cancel('form')">取 消</el-button>-->
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>

    <!--    <panel-group @handleSetLineChartData="handleSetLineChartData" />-->

    <!--    <el-row>-->
    <!--      <el-col :span="6">-->
    <!--        <el-card class="box-card">-->
    <!--          <div slot="header" class="clearfix">-->
    <!--            <span>开源作者：yukong</span>-->
    <!--            <el-button style="float: right; padding: 3px 0" type="text" @click="goGithub">github地址</el-button>-->
    <!--          </div>-->
    <!--          <el-timeline>-->
    <!--            <el-timeline-item v-for="(activity, index) in activities2" :key="index" :icon="activity.icon" :type="activity.type" :color="activity.color" :size="activity.size" :timestamp="activity.timestamp">-->
    <!--              {{activity.content}}-->
    <!--            </el-timeline-item>-->
    <!--          </el-timeline>-->
    <!--        </el-card>-->

    <!--      </el-col>-->

    <!--      <el-col :span="16" :offset="2">-->
    <!--        <el-card class="box-card">-->
    <!--          <div slot="header" class="clearfix">-->
    <!--            <span>访问趋势图</span>-->
    <!--          </div>-->
    <!--          <line-chart :chart-data="lineChartData" />-->
    <!--        </el-card>-->
    <!--      </el-col>-->
    <!--    </el-row>-->
  </div>
</template>

<script>
import { getToken, setToken, removeToken } from '@/utils/auth'
import { putNewPass } from '@/api/user'
import { setStore, getStore } from '@/utils/store'
export default {
  name: 'DashboardAdmin',
  // components: {
  //   PanelGroup,
  //   LineChart
  // },
  data() {
    // const validateOldPass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入旧密码'))
    //   } else {
    //     callback()
    //   }
    // }
    const validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!/\d/.test(value)) {
        callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
      } else if (!/[a-z]/.test(value)) {
        callback(new Error('密码需包含字母以及数字，密码长度在 8 到 20 个字符'))
      } else if (this.form.oldPassword !== undefined) {
        if (value === this.form.oldPassword) {
          callback(new Error('新密码不能与旧密码相同'))
        } else {
          callback() // 必须确保调用该方法 才会有返回值
        }
      } else {
        // if (this.form.newPassword !== '') {
        //   this.$refs.form.validateField('confirmNewPassword')
        // }
        callback()
      }
    }
    const validateConfirmNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (this.form.newPassword === undefined) {
        this.$refs.form.validateField('newPassword')
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false,
      textMap: {
        update: '首次登录请修改密码',
        create: '创建',
      },
      dialogStatus: 'update',
      tableData: [
        {
          userName: this.$store.state.user.name,
          date: this.$store.state.user.lastLogtime,
          loginIp: this.$store.state.user.lastLogip,
        },
      ],
      form: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmNewPassword: undefined,
      },
      rules: {
        oldPassword: [
          {
            required: true,
            message: '请输入旧密码',
            trigger: 'blur',
          },
        ],
        newPassword: [
          {
            required: true,
            message: '请输入新密码',
            trigger: 'blur',
          },
          {
            min: 8,
            max: 20,
            message: '密码长度在 8 到 20 个字符',
            trigger: 'blur',
          },
          {
            validator: validateNewPass,
            trigger: 'blur',
          },
        ],
        confirmNewPassword: [
          {
            required: true,
            message: '请再次输入新密码',
            trigger: 'blur',
          },
          {
            min: 8,
            max: 20,
            message: '密码长度在 8 到 20 个字符',
            trigger: 'blur',
          },
          {
            validator: validateConfirmNewPass,
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    const query = location.search.substring(1)
    if (query) {
      const token = query.split('=')
      sessionStorage.setItem('tokenSign', token[1])
      setToken(token[1])
      // commit('SET_TOKEN', token[1])
    }
    if (
      this.$store.state.user.isInitialLogin === null ||
      this.$store.state.user.isInitialLogin === 1
    ) {
      this.dialogFormVisible = true
      // Cookies.set('firstLogin', true)
      // const firstLogin = Cookies.get('firstLogin')
      // if (firstLogin) {
      // this.$message('首次登录请修改密码')
      // this.dialogFormVisible = true
      // this.$router.push({
      //   path: '/admin/newpass'
      // })
      // }
    }
    // else {
    //   const firstLogin = Cookies.get('firstLogin')
    //   if (firstLogin) {
    //     // this.$message('首次登录请修改密码')
    //     this.dialogFormVisible = true
    //     // this.$router.push({
    //     //   path: '/admin/newpass'
    //     // })
    //   }
    // }
  },
  methods: {
    ai_dialog_close() {
      return false
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          putNewPass(this.form).then((response) => {
            if (response.message === 'success') {
              setStore({
                name: 'isInitialLogin',
                content: 0,
                type: 'session',
              })
              this.setFormEmpty()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000,
              })
            } else {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'success',
                duration: 2000,
              })
              console.log(response.message)
            }
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.setFormEmpty()
    },
    setFormEmpty() {
      this.form = {
        oldPassword: undefined,
        newPassword: undefined,
        confirmNewPassword: undefined,
      }
    },
  },
  // data() {
  //   return {
  //     lineChartData: lineChartData.newVisitis,
  //     activities2: [{
  //       content: 'v1版本发布',
  //       timestamp: '2019-04-25 20:46',
  //       size: 'large',
  //       type: 'primary',
  //       icon: 'el-icon-more'
  //     }, {
  //       content: '支持动态路由',
  //       timestamp: '2019-02-03 20:46',
  //       color: '#0bed87'
  //     }, {
  //       content: '支持oauth2 client管理',
  //       timestamp: '2019-01-03 20:46',
  //       color: '#0bed87'
  //     }, {
  //       content: '支持终端管理,代码生成，一键curd',
  //       timestamp: '2019-01-03 20:46',
  //       color: '#0bdd87'
  //     }, {
  //       content: '支持token管理',
  //       timestamp: '2018-12-03 20:46',
  //       color: '#0bcd87'
  //     }, {
  //       content: '完善字典管理等功能',
  //       timestamp: '2018-10-03 20:46',
  //       color: '#0bbd87'
  //     }, {
  //       content: '完善手机登录',
  //       timestamp: '2018-04-26 20:46',
  //       size: 'large'
  //     }, {
  //       content: '正式开源panda-cloud',
  //       timestamp: '2018-04-03 20:46'
  //     }]
  //   }
  // },
  // methods: {
  //   handleSetLineChartData(type) {
  //     this.lineChartData = lineChartData[type]
  //   },
  //   goGithub() {
  //     location.href = 'https://github.com/YuKongEr/panda-cloud'
  //   }
  // }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}
</style>
